from __future__ import print_function
from prepross import *
from prepross2 import *
import numpy as np
import keras
import pickle
from keras.preprocessing.image import ImageDataGenerator
from keras.models import Sequential
from keras.layers import Dense, Dropout, Activation, Flatten
from keras.layers import Conv2D, MaxPooling2D
from keras.engine.training import Model
from keras.optimizers import SGD,Adam
batch_size = 12
num_classes = 2
epochs = 200

data_augmentation = False
'''
x_train_new=datafile(picdirec)
with open('training_data/x_train.pkl','wb') as file:
	pickle.dump(x_train_new, file)
print('x done!')
y=labelfile(indirec)
print('1')
label_result,intensity_result=multilabel(indirec,y)
#y_train=label_result[:,0]
print('y done')
y_label=[]
y_intensity=[]
j=0
for i in range(len(x_train)):
	for t in range(0,len(y)):
		if x_train[i][0]==y[t][0]:
			y_label.append(label_result[t,:])
			y_intensity.append(intensity_result[t,:])
			j=t

x_train = np.asarray(x_train_new)
y_label = np.asarray(y_label)
y_intensity = np.asarray(y_intensity)
print(x_train.shape)
print(y_label.shape)
print(y_intensity.shape)
with open('training_data/y_train_intensity.pkl','wb') as file:
	pickle.dump(y_intensity, file)
with open('training_data/y_train_label.pkl','wb') as file:
	pickle.dump(y_label, file)
#with open('training_data/x_train.pkl','wb') as file:
#	pickle.dump(x_train, file)
print('done!')
'''
x_train = pickle.load(open('training_data/x_train.pkl', 'rb'))
y_train = pickle.load(open('training_data/y_train_label.pkl', 'rb'))

segment_val = int(len(x_train) * 0.6)
segment_test = int(len(x_train) * 0.8)
try:
   x_val = np.asarray(x_train[segment_val:segment_test], dtype='float32') / 255
   y_val = np.asarray(y_train[segment_val:segment_test], dtype='float32')
   x_test = np.asarray(x_train[segment_test:], dtype='float32') / 255
   y_test = np.asarray(y_train[segment_test:], dtype='float32')
except:
   pass

#print(y_train)

x_train = np.asarray(x_train[:segment_val], dtype='float32') / 255
y_train = np.asarray(y_train[:segment_val], dtype='float32')

#y_train = keras.utils.to_categorical(y_train, num_classes)
#y_val = keras.utils.to_categorical(y_val, num_classes)
#y_test = keras.utils.to_categorical(y_test, num_classes)

model = Sequential()

model.add(Conv2D(32, (3,3), padding = 'same', input_shape = x_train.shape[1:], kernel_regularizer=keras.regularizers.l2(0.1)))
model.add(Activation('relu'))
model.add(Conv2D(32, (3,3)))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2,2)))
model.add(Dropout(0.25))

model.add(Conv2D(32, (3,3), padding = 'same'))
model.add(Activation('relu'))
model.add(Conv2D(32, (3,3)))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2,2)))
model.add(Dropout(0.25))

model.add(Flatten())
model.add(Dense(512))
model.add(Activation('relu'))
model.add(Dropout(0.5))

fs = []
for i in range(13):
	f0 = Sequential(name = 'f'+str(i))
	f0.add(model)
	f0.add(Dense(128))
	f0.add(Activation('relu'))
	f0.add(Dense(1))
	f0.add(Activation('sigmoid'))
	#f12=f11=f10=f9=f8=f7=f6=f5=f4=f3=f2=f1=f0
	fs.append(f0)	
outputs = []
for i in range(13):
	outputs.append(fs[i].output)
final_model = Model(model.input, outputs, name = 'final_model')
# initiate RMSprop optimizer
opt = keras.optimizers.rmsprop(lr=0.00001, decay = 1e-6)

loss = []
metrics = []
for i in range(13):
	loss.append('binary_crossentropy')
	metrics.append('binary_accuracy')
final_model.compile(loss = 'binary_crossentropy', optimizer = opt, metrics = ['binary_accuracy'])

checkpoint = keras.callbacks.ModelCheckpoint(filepath = '/data02/log/checkpoint-{epoch:02d}-{loss:.5f}.hdf5')
print(y_train.shape)
_y = []
_y_val = []
for i in range(13):
	print(y_train[:,i].shape)
	_y.append(y_train[:,i])
	_y_val.append(y_val[:,i])
final_model.fit(x_train, _y, batch_size = batch_size, epochs = epochs, validation_data = (x_val, _y_val), shuffle = True, callbacks = [checkpoint])

#print(f0.predict(x_test))
